package com.zhudoo.zhilian.service;

import com.alibaba.fastjson.JSON;
import com.zhudoo.zhilian.dao.entity.Tip;
import com.zhudoo.zhilian.dao.entity.User;
import com.zhudoo.zhilian.dao.impl.ChatDao;
import com.zhudoo.zhilian.dao.impl.CityDao;
import com.zhudoo.zhilian.dao.impl.TipDao;
import com.zhudoo.zhilian.dao.impl.UserDao;
import com.zhudoo.zhilian.util.DateUtil;
import com.zhudoo.zhilian.util.service.TipUtil;
import com.zhudoo.zhilian.util.service.UserUtil;
import com.zhudoo.zhilian.util.web.BusinessException;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author zhudao
 */
@Service
public class TipService {
    @Autowired
    TipDao tipDao;
    @Autowired
    UserDao userDao;
    @Autowired
    CityDao cityDao;
    @Autowired
    ChatDao chatDao;

    /**
     * 求租的帖子
     */
    public static int TIP_TYPE_1 = 1;
    public static int TIP_TYPE_2 = 2;
    public static int TIP_TYPE_3 = 3;
    public static int TIP_TYPE_4 = 4;


    public static Map<Integer, String> TYPE_NAME_MAP = new HashMap() {
        {
            put(TIP_TYPE_1, "求租");
            put(TIP_TYPE_2, "转租");
            put(TIP_TYPE_3, "找室友");
            put(TIP_TYPE_4, "出租");
        }
    };

    /**
     * 默认
     */
    public static int TIP_STATUS_1 = 1;

    /**
     * 已取消
     */
    public static int TIP_STATUS_2 = 2;


    public static Map TYPE_STATUS_MAP = new HashMap() {
        {
            put(TIP_STATUS_1, "默认");
            put(TIP_STATUS_2, "已取消");
        }
    };


    public static Map HALL_MAP = new HashMap() {
        {
            put(1, "一室");
            put(2, "两室");
            put(3, "三室");
            put(4, "四室");
            put(5, "五室");
        }
    };

    public static Map ROOM_TYPE_MAP = new HashMap() {
        {
            put(1, "整租");
            put(2, "合租");
        }
    };

    /**
     * 入住时间升序
     */
    public static int ASE = 1;


    public List<Map<String, Object>> getTipList(ServiceContext serviceContext) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Tip> tipList = tipDao.getTipList(serviceContext.getPage(), serviceContext.getLimit(), serviceContext.getParams());
        for (Tip tip : tipList) {
            Map<String, Object> resultMap = new HashMap<>(10);
            resultMap.put("tipId", tip.getId());
            resultMap.put("tipType", tip.getType());
            resultMap.put("tipTypeName", TYPE_NAME_MAP.get(tip.getType()));
            resultMap.put("userId", tip.getUserId());
            resultMap.put("price", tip.getPrice());
            resultMap.put("checkInDate", DateUtil.parseToString(tip.getCheckInDate(), DateUtil.DATEFORMAT_YYYYMMDD));
            resultMap.put("stationName", tip.getStationName());
            resultMap.put("lineName", tip.getLineName());
            resultMap.put("roomTypeName", ROOM_TYPE_MAP.get(tip.getRoomType()));
            resultMap.put("hallName", HALL_MAP.get(tip.getHall()));
            resultMap.put("description", tip.getDescription());
            resultMap.put("viewCount", tip.getViewCount());
            resultMap.put("status", tip.getStatus());
            User user = userDao.findUserById(tip.getUserId());
            resultMap.put("nickName", user.getNickName());
            resultMap.put("headImg", user.getHeadImg());
            resultMap.put("sex", user.getSex());
            resultMap.put("status", tip.getStatus());
            resultMap.put("pictures", JSON.parseArray(tip.getPictures()));
            resultMap.put("statusName", TipService.TYPE_STATUS_MAP.get(tip.getStatus()));
            resultList.add(resultMap);
        }
        return resultList;
    }

    public Map<String, Object> getTipId(ServiceContext serviceContext) {
        Integer tipId = serviceContext.getParamsInteger("tipId");
        String token = serviceContext.getParamsString("token");
        Tip tip = tipDao.getTipById(tipId);
        tipDao.addViewCount(tipId, tip.getViewCount() + 1);
        Map<String, Object> resultMap = new HashMap<>(10);
        resultMap.put("tipId", tip.getId());
        resultMap.put("tipType", tip.getType());
        resultMap.put("tipTypeName", TYPE_NAME_MAP.get(tip.getType()));
        resultMap.put("userId", tip.getUserId());
        resultMap.put("cityId", tip.getCityId());
        resultMap.put("title", tip.getTitle());
        resultMap.put("price", tip.getPrice());
        resultMap.put("checkInDate", DateUtil.parseToString(tip.getCheckInDate(), DateUtil.DATEFORMAT_YYYYMMDD));
        resultMap.put("station", tip.getStation());
        resultMap.put("stationName", tip.getStationName());
        resultMap.put("line", tip.getLine());
        resultMap.put("lineName", tip.getLineName());
        resultMap.put("roomType", tip.getRoomType());
        resultMap.put("roomTypeName", ROOM_TYPE_MAP.get(tip.getRoomType()));
        resultMap.put("hall", tip.getHall());
        resultMap.put("hallName", HALL_MAP.get(tip.getHall()));
        resultMap.put("description", tip.getDescription());
        resultMap.put("viewCount", tip.getViewCount());
        resultMap.put("status", tip.getStatus());
        resultMap.put("createTime", DateUtil.parseToString(tip.getCreateTime(), DateUtil.DATEFORMAT_YYYYMMDDHHMMSS));
        User tipUser = userDao.findUserById(tip.getUserId());
        resultMap.put("nickName", tipUser.getNickName());
        resultMap.put("headImg", tipUser.getHeadImg());
        resultMap.put("sex", tipUser.getSex());
        resultMap.put("status", tip.getStatus());
        resultMap.put("pictures", JSON.parseArray(tip.getPictures()));
        resultMap.put("statusName", TipService.TYPE_STATUS_MAP.get(tip.getStatus()));
        //是否编辑
        Long countByTipId = chatDao.countByTipId(tipId);
        resultMap.put("allowEdit", TipUtil.getAllowEdit(countByTipId, tip));
        User user = userDao.findUserByToken(token);
        //显示联系按钮
        resultMap.put("showContact", TipUtil.getShowContact(user, tip));
        //显示约看按钮
        resultMap.put("showAppointment", TipUtil.getShowAppointment(user, tip));
        //用户身份提示
        resultMap.put("userTypeBrief", UserUtil.getUserTypeBrief(tipUser));
        return resultMap;
    }

    public List<Map<String, Object>> getTipByUserId(ServiceContext serviceContext) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String token = serviceContext.getParamsString("token");
        Integer heUserId = serviceContext.getParamsInteger("heUserId");
        Integer page = serviceContext.getPage();
        Integer limit = serviceContext.getLimit();
        if (heUserId == null) {
            User user = userDao.findUserByToken(token);
            heUserId = user.getId();
        }
        List<Tip> tipList = tipDao.getTipByUserIdAndValid(heUserId, true, page, limit);
        for (Tip tip : tipList) {
            Map<String, Object> resultMap = new HashMap<>(10);
            resultMap.put("tipId", tip.getId());
            resultMap.put("tipType", tip.getType());
            resultMap.put("tipTypeName", TYPE_NAME_MAP.get(tip.getType()));
            resultMap.put("userId", tip.getUserId());
            resultMap.put("price", tip.getPrice());
            resultMap.put("checkInDate", DateUtil.parseToString(tip.getCheckInDate(), DateUtil.DATEFORMAT_YYYYMMDD));
            resultMap.put("type", tip.getType());
            resultMap.put("typeName", TYPE_NAME_MAP.get(tip.getType()));
            resultMap.put("stationName", tip.getStationName());
            resultMap.put("lineName", tip.getLineName());
            resultMap.put("roomTypeName", ROOM_TYPE_MAP.get(tip.getRoomType()));
            resultMap.put("hallName", HALL_MAP.get(tip.getHall()));
            resultMap.put("description", tip.getDescription());
            resultMap.put("viewCount", tip.getViewCount());
            resultMap.put("status", tip.getStatus());
            resultMap.put("statusName", TipService.TYPE_STATUS_MAP.get(tip.getStatus()));
            resultList.add(resultMap);
        }
        return resultList;

    }

    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> addTip(ServiceContext serviceContext) {
        Map<String, Object> resultMap = new HashMap<>(1);
        String token = serviceContext.getParamsString("token");
        Integer cityId = serviceContext.getParamsInteger("cityId");
        Integer tipType = serviceContext.getParamsInteger("tipType");
        Integer tipId = serviceContext.getParamsInteger("tipId");
        Integer line = serviceContext.getParamsInteger("line");
        String lineName = serviceContext.getParamsString("lineName");
        Integer station = serviceContext.getParamsInteger("station");
        String stationName = serviceContext.getParamsString("stationName");
        Integer hall = serviceContext.getParamsInteger("hall");
        Integer roomType = serviceContext.getParamsInteger("roomType");
        Integer price = serviceContext.getParamsInteger("price");
        String checkInDate = serviceContext.getParamsString("checkInDate");
        String description = serviceContext.getParamsString("description");
        String pics = serviceContext.getParamsString("pics");
        User user = userDao.findUserByToken(token);
        String onlyStr = TipUtil.getOnlyStr(user.getId(), tipType, station, hall, roomType, price, checkInDate);
        Tip tip;
        if (tipId != null) {
            tip = tipDao.getTipById(tipId);
        } else {
            tip = tipDao.getTipByOnlyStr(onlyStr);
            if (tip != null) {
                throw BusinessException.getInstance("请勿发布相同的内容,严重者会封号的哦");
            }
            tip = new Tip();
            tip.setViewCount(1);
            tip.setCreateTime(new Date());
            tip.setValid(true);
            tip.setOnlyStr(onlyStr);
            tip.setStatus(TIP_STATUS_1);
            tip.setType(tipType);
        }
        tip.setCheckInDate(DateUtil.parseToDate(checkInDate, DateUtil.DATEFORMAT_YYYYMMDD));
        tip.setStation(station);
        tip.setStationName(stationName);
        tip.setLine(line);
        tip.setTitle(TipUtil.getTitle(tipType, lineName, stationName));
        tip.setLineName(lineName);
        tip.setPrice(price);
        tip.setHall(hall);
        tip.setPictures(pics);
        tip.setRoomType(roomType);
        tip.setCityId(cityId);
        tip.setUserId(user.getId());
        tip.setDescription(description);
        tip.setUpdateTime(new Date());
        tipDao.saveTip(tip);
        resultMap.put("tipId", tip.getId());
        return resultMap;
    }

    public void cancelTip(ServiceContext serviceContext) {
        String token = serviceContext.getParamsString("token");
        Integer tipId = serviceContext.getParamsInteger("tipId");
        User user = userDao.findUserByToken(token);
        Tip tip = tipDao.getTipById(tipId);
        if (user.getId().intValue() == tip.getUserId()) {
            tip.setStatus(TipService.TIP_STATUS_2);
            tipDao.saveTip(tip);
        }
    }

    public void shareTip(ServiceContext serviceContext) {
        Integer tipId = serviceContext.getParamsInteger("tipId");
        Tip tip = tipDao.getTipById(tipId);
        tip.setUpdateTime(new Date());
        tipDao.saveTip(tip);
    }
}
